package com.frxuexi.visit.view

import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.adapter.StatusAdapter
import com.frxuexi.visit.adapter.VisitAdapter
import com.frxuexi.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitIntent, VisitViewModel>() {
    override val binding: ActivityVisitBinding by lazy { ActivityVisitBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()

    val statusAdapter by lazy { StatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val items = it.data as Collection<VisitBeanItem>
                        visitAdapter.data.clear()
                        if(items.size==0){
                            vm.sendIntent(VisitIntent.getVisitsByPurpose(binding.visitSv.text.toString()))
                        }else {
                            visitAdapter.addData(items.sortedByDescending { it.id })
                            visitAdapter.notifyDataSetChanged()
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val status= mutableListOf<StatusBean>(
            StatusBean("全部状态",true),
            StatusBean("待审核",false),
            StatusBean("已通过",false),
            StatusBean("驳回",false),
            StatusBean("已完成",false)
        )
        statusAdapter.addData(status)
        binding.visitRvStatus.apply {
            adapter=statusAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity,RecyclerView.HORIZONTAL,false)
        }

        statusAdapter.setOnItemClickListener { adapter, view, position ->
            statusAdapter.data.forEach {
                it.isCheck=false
            }
            statusAdapter.data.get(position).isCheck=true
            statusAdapter.notifyDataSetChanged()

            if(position==0){
                vm.sendIntent(VisitIntent.getVisits)
            }else{
                vm.sendIntent(VisitIntent.getVisitsByStatus(status.get(position).title))
            }
        }

        binding.visitAdd.setOnClickListener {
            ArouterManager.nav(this,"/visit/VisitAddActivity")
        }

        binding.visitRv.apply {
            adapter=visitAdapter
            layoutManager=LinearLayoutManager(this@VisitActivity)
        }

        binding.visitSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                 vm.sendIntent(VisitIntent.getVisitsByName(s.toString()))
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/visit/VisitApproveActivity",visitAdapter.data.get(position).id!!)
        }


    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(VisitIntent.getVisits)
    }

}